package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.service.IOrderGoodsDetailService;
import cn.leyaber.hrm.domain.OrderGoodsDetail;
import cn.leyaber.hrm.query.OrderGoodsDetailQuery;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderGoodsDetail")
public class OrderGoodsDetailController {
    @Autowired
    public IOrderGoodsDetailService orderGoodsDetailService;

    /**
    * 保存和修改公用的
    * @param orderGoodsDetail  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody OrderGoodsDetail orderGoodsDetail){
        try {
            if(orderGoodsDetail.getId()!=null){
                orderGoodsDetailService.updateById(orderGoodsDetail);
            }else{
                orderGoodsDetailService.insert(orderGoodsDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderGoodsDetailService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public OrderGoodsDetail get(@PathVariable("id")Long id)
    {
        return orderGoodsDetailService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<OrderGoodsDetail> list(){

        return orderGoodsDetailService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<OrderGoodsDetail> json(@RequestBody OrderGoodsDetailQuery query)
    {
        Page<OrderGoodsDetail> page = new Page<OrderGoodsDetail>(query.getPage(),query.getRows());
        page = orderGoodsDetailService.selectPage(page);
        return new PageList<OrderGoodsDetail>(page.getTotal(),page.getRecords());
    }
}
